package com.xxl.sso.server.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/file")
public class FileController {
	@RequestMapping("/logo")
	public void downloadFile(HttpServletRequest request, HttpServletResponse response) {
		String path= request.getParameter("path");
		File file = new File(path);
		// 后缀名
//		String suffixName = path.substring(path.lastIndexOf("."));
		// 判断文件是否存在如果不存在就返回默认图标
		if (!(file.exists() && file.canRead())) {
			file = new File(request.getSession().getServletContext().getRealPath("/") + "WEB-INF/classes/static/theme/img/cloud.jpg");
		}
		FileInputStream inputStream = null;
		try {
			inputStream = new FileInputStream(file);
			byte[] data = new byte[(int) file.length()];
			int length = inputStream.read(data);
			inputStream.close();
			// setContentType("text/plain; charset=utf-8"); 文本
//			response.setContentType("image/png;charset=utf-8");
			OutputStream stream = response.getOutputStream();
			stream.write(data);
			stream.flush();
			stream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
